package com.hxx.day2.common;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.UUID;
import java.util.zip.CRC32;

public class TestDemo {

    /**
     * 对象创建在常量池中
     * 并且，常量池中的变量不允许重复存在的
     */
    @Test
    public void test1() {
        String str1 = "a";
        String str2 = "a";

        System.out.println(str1 == str2);
    }

    /**
     *  使用new调用构造方法就会在堆中和常量池中创建对象
     */
    @Test
    public void test2() {
        String str1 = "a";
        String str2 = new String("a");

        System.out.println(str1 == str2);
    }

    /**
     * 共创建了两个对象
     */
    @Test
    public void test3() {
        String s1 = "abc";

        String s2 = "abc";

        String s3 = new String("abc");

        System.out.println("s1==s2, " + (s1==s2));
        System.out.println("s2==s3, " + (s2==s3));
        System.out.println("s2 equlas s3," + (s2.equals(s3)));
    }

    /**
     * 创建了三个对象
     * 两个在堆中，一个在常量池
     */
    @Test
    public void test4() {
        String s1 = new String("hello") ;
        String s2 = new String("hello") ;
        String s3 = "hello";

        System.out.println("s1==s2, " + (s1==s2));
    }
    /**
     * string的方法
     */
    @Test
    public void test5() {
        String s1 = "hello";
        String s2 = "Hello";
        System.out.println(s1.equals(s2));
        System.out.println(s1.equalsIgnoreCase(s2));
    }
    /**
     * 获取第一次出现的索引
     * 获取最后一次出现的索引
     */
    @Test
    public void test6() {
        String s1 = "abcdefg";
        System.out.println(s1.indexOf("c"));
        //获取最后一次出现的索引
        String fliename = "d:/a.exe.txt";
        int index = fliename.lastIndexOf(".");
        String ext = fliename.substring(index);
        //使用uuid，crc32生成文件名
        String string = UUID.randomUUID().toString();
        CRC32 crc32 = new CRC32();
        crc32.update(string.getBytes());
        System.out.println(crc32.getValue()+ext);
    }

    /**
     * 替换字符串
     */
    @Test
    public void test7() {
        String s1 = "ab33c3de3f3g";
        System.out.println(s1.replace("3",""));
        System.out.println(s1.replace("3","-"));
    }
    /**
     * 使用正则表达式
     */
    @Test
    public void test8() {
        String s1 = "asdd33dfsdaf33ddsd55fdd3dssf4343sdf455ddsdddh565gggh55ddhg";

        //将dd替换为"中"
        System.out.println(s1.replaceAll("dd", "中"));
        System.out.println(s1.replaceAll("d{2}", "中"));
    }
    /**
     * 拆分字符串
     */
    @Test
    public void test9() {
        String s1 = "a,b,c,d,e,f,g";
        String[] split = s1.split(",");
        Arrays.stream(split).forEach(System.out::println);
    }

    /**
     * 去前后空格
     */
    @Test
    public void test10() {
        String s1 = "  he llo  ";
        System.out.println(s1.trim().length());
        System.out.println(s1.length());
        System.out.println(s1.replace(" ","").length());
    }

    /**
     * 类型转换
     */
    @Test
    public void test11() {
        String s1 = "123";
        System.out.println(String.valueOf(123));
        System.out.println(Integer.parseInt(s1));
        System.out.println(Integer.valueOf(s1));
    }

    /**
     * 拼接字符串
     */
    @Test
    public void test12() {
        String s1 = "hello";
        String s2 = "world";
        System.out.println(String.join("-", s1,s2));
    }

    /**
     * 正则表达式的使用
     * \d 匹配数字
     * \D 匹配非数字
     * \s 匹配空白字符
     * \S 匹配非空白字符
     * \w 匹配字母数字下划线
     * \W 匹配非字母数字下划线
     */
    @Test
    public void test13() {
        String s1 = "asdd33dfsdaf33ddsd55fdd3dssf4343sdf455ddsdddh565gggh55ddhg";

        //
        System.out.println(s1.replaceAll("\\D", "中"));

        String s2 = "sdf,dsfsfd;sdf.sdf";
        System.out.println(s2.replaceAll("\\W", ""));
    }

}






















